package com.tencent.liteav.demo.livepusher.camerapush.model;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.tencent.liteav.demo.livepusher.R;

public class CommonDialogEmpty extends Dialog {

    public CommonDialogEmpty(Context context) {
        super(context);
    }

    public CommonDialogEmpty(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public CommonDialogEmpty(Context context, int theme) {
        super(context, theme);
    }

    /**
     * 每次使用必须new一个builder，因为初始化界面在builder构造函数里
     */
    public static class Builder {
        private CommonDialogEmpty mDialog;
        private Context context;
        private View mView;//dialog视图
        private LinearLayout mContainer;
        private View outView;
        private boolean canBack = true;//是否可以返回键取消，默认可以
        private boolean canTouch = true;//是否可以点击外部取消，默认可以

        public Builder(Context context) {
            this.context = context;
            initView();
        }

        /**
         * 初始化视图
         */
        private void initView() {
            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mView = mInflater.inflate(R.layout.dialog_common_empty, null);
            mContainer = mView.findViewById(R.id.dialog_common_empty_content);
            outView = mView.findViewById(R.id.dialog_common_empty_outview);
            outView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Builder.this.closeDialog();
                }
            });
        }

        /**
         * 设置内容
         */
        public Builder setContainer(View view) {
            mContainer.addView(view);
            return this;
        }

        /**
         * 设置不可以返回键取消
         */
        public Builder noCancleBack() {
            canBack = false;
            return this;
        }

        /**
         * 设置不可以点击外部取消
         */
        public Builder noCancleTouch() {
            canTouch = false;
            return this;
        }

        public Dialog getmDailog() {
            return mDialog;
        }

        /**
         * 关闭dialog
         */
        public void closeDialog() {
            if (mDialog != null) {
                mDialog.dismiss();
            }
        }

        public CommonDialogEmpty create() {
            mDialog = new CommonDialogEmpty(context, R.style.TransparentDialog);
            mDialog.setContentView(mView);
            //设置dialog的大小
            android.view.WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = lp.MATCH_PARENT;//dp
            lp.height = lp.MATCH_PARENT;
            mDialog.getWindow().setAttributes(lp);
            mDialog.setCancelable(canBack);
            mDialog.setCanceledOnTouchOutside(canTouch);
            return mDialog;
        }

        public boolean isShowing() {
            if (mDialog != null && mDialog.isShowing()) {
                return true;
            }
            return false;
        }
    }

    public static View getStopView(Context c, final StopCallback listener) {
        View view = LayoutInflater.from(c).inflate(R.layout.dialog_content_stop, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        view.findViewById(R.id.tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(true);
            }
        });
        view.findViewById(R.id.tv_continue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(false);
            }
        });
        return view;
    }

    public interface StopCallback {
        void onClick(boolean isStop);
    }

}
